package com.enterprise.web;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.assg2.dao.*;
import com.movie.Owner;
import com.movie.Viewer;
import javax.servlet.http.HttpSession;

/**
 * This is the command that will be used for logging in users.
 * If logon is successful, the command should place a list of contacts
 * in the request attriubutes.
 * @author  yunki
 */
public class LoginCommand implements Command {

    private String typeofuser;

    public String getTypeofuser() {
        return typeofuser;
    }

    public void setTypeofuser(String typeofuser) {
        this.typeofuser = typeofuser;
    }

    /** Creates a new instance of LoginCommand */
    public LoginCommand() {
    }

    public String execute(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //TODO: set the response content type to "text/html" here
        //TODO: You'll have to use getWriter() to output some HTML texts
        //TODO: close the output stream

        response.setContentType("text/html");// from response, set content type
        PrintWriter out = response.getWriter();// from response, get output writer
        
        
        HttpSession session = request.getSession(true);
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        Object user = (Object) session.getAttribute("user");
        String usertype = (String) session.getAttribute("usertype");

        if ((session.getAttribute("user") != null) && (usertype.matches("owner"))) {
           // request.setAttribute("message", "You are login as a "+usertype+" <a href='index.jsp'>Go to index</a>");
            return "/owner.jsp";
        }
        if ((session.getAttribute("user") != null) && (usertype.matches("viewer"))) {
            //request.setAttribute("message", "You are login as a "+usertype+" <a href='viewer.jsp'>Go to index</a>");
            return "/viewer.jsp";
            
        }


        if (user == null) {
            if (username != null || password != null) {
                user = User(username, password);
                if (user != null) {
                    session.setAttribute("user", user);
                    session.setAttribute("usertype", getTypeofuser());
                    request.setAttribute("message", "Login Succesfull:<a href='dispatcher?operation=login'>Go to index</a>");
                    return "message";
                    //return "/index.jsp";
                } else {
                    return "/loginfailed.jsp";
                }
            } else {
                return "/login.jsp";
            }
        } else {
            return "/index.jsp";
        }

    }

    protected Object User(String username, String password) {
        
        DaoOwner daoowner = new DaoOwner();
        Owner owner = daoowner.findByUsername(username);

        if (owner != null) {
            if (owner.getPassword().matches(password)) {
                setTypeofuser("owner");
                return owner; // trust everyone
            }
        }
        
        DaoViewer daoviewer = new DaoViewer();
        Viewer viewer = (Viewer) daoviewer.findBy("username", username);

        if (viewer != null) {
            if (viewer.getPassword().matches(password)) {
                setTypeofuser("viewer");
                return viewer;
            }
        }
        return null;
    }
}
